<template>
	<el-dialog title="问题反馈对话框" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="关联任务" prop="processId">
				<el-select
					v-model="form.processId"
					filterable
					remote
					reserve-keyword
					placeholder="请输入关键词"
					:remote-method="remoteMethod"
					:loading="loading"
					style="width:100%"
				>
					<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="我的问题" prop="remark">
				<el-input v-model="form.remark" placeholder="我的问题" clearable maxlength="380" type="textarea" :rows="5"></el-input>
			</el-form-item>
			<el-form-item label="相关附件" prop="attachId">
				<sc-upload-file :apiObj="apiObj"  v-model="fileList" :data="{module:'supervision'}" :showFileList="false" @success="addNewFile">
					<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
				</sc-upload-file>
				<attach-menu v-model:attachIds="form.attachId" :roleFlag="4" :preview="true"></attach-menu>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>
import AttachMenu from "@/views/module/attachMenu";

export default {
	emits: ['success', 'closed'],
	components: {
		AttachMenu
	},
	data() {
		return {
			mode: "add",
			loading:false,
			visible: false,
			isSaveing: false,
			isShowMsg:false,
			fileList:[],
			apiObj:this.$API.file.attach.uploadFile,
			form: {
				recordId:"",
				processId:'',
				remark: '',
				attachId:''
			},
			options: [],
			rules: {
				processId: [
					{required: true, message: '关联任务不能为空', trigger: 'blur'}
				],
				content:[
					{required: true, message: '问题不能为空', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.supervision.supervisionProblem.insertSupervisionProblem.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.supervision.supervisionProblem.updateSupervisionProblem.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		addNewFile(res, file)
		{
			let tArr =[];
			if(this.form.attachId!=undefined&&this.form.attachId!=null&&this.form.attachId!=''){
				tArr = this.form.attachId.split(",");
			}
			tArr.push(res.data.attachId);
			this.form.attachId = tArr.join(",");
		},
		async remoteMethod(keyword) {
			let res = await this.$API.supervision.supervisionProcess.getSupervisionProcessForSelect.get({keyword: keyword});
			if (res.code == 200) {
				this.options = res.data;
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async getSupervisionProcessForSelect(processId)
		{
			let res = await this.$API.supervision.supervisionProcess.getSupervisionProcessForSelect.get({processId: processId});
			if (res.code == 200) {
				this.options = res.data;
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		setData(data){
			this.getSupervisionProcessForSelect(data.processId);
			this.form.recordId = data.recordId
			this.form.processId = data.processId
			this.form.remark = data.remark
			this.form.attachId = data.attachId
		},
	}
}
</script>

<style>
.customWidth {
	width: 50%;
}
</style>
